home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / games / larn12s.arc / LARN.ARC / TOK.C < prev   
C/C++ Source or Header  |  1987-10-28  |  8KB  |  317 lines

  1. /* tok.c        Larn is copyrighted 1986 by Noah Morgan. */
  2. #ifdef GEMDOS
  3. #include <types.h>
  4. #else
  5. #include <sys/types.h>
  6. #ifdef SYSV
  7. #include <fcntl.h>
  8. # ifndef MSDOS
  9. #   include <termio.h>
  10. # endif
  11. #else SYSV
  12. #include <sys/ioctl.h>
  13. #endif SYSV
  14. #endif GEMDOS
  15. #include "header.h"
  16.  
  17. # define CHKPTINT    400
  18.  
  19. static char lastok=0;
  20. int yrepcount=0,dayplay=0;
  21. #ifndef FLUSHNO
  22. #define FLUSHNO 5
  23. #endif FLUSHNO
  24. static int flushno=FLUSHNO;    /* input queue flushing threshold */
  25. #define MAXUM 52    /* maximum number of user re-named monsters */
  26. #define MAXMNAME 40    /* max length of a monster re-name */
  27. static char usermonster[MAXUM][MAXMNAME]; /* the user named monster name goes here */
  28. static char usermpoint=0;            /* the user monster pointer */
  29.  
  30. /*
  31.     lexical analyzer for larn
  32.  */
  33. yylex()
  34.     {
  35.     char cc;
  36.     int ic;
  37.     if (hit2flag) { hit2flag=0;  yrepcount=0;  return(' '); }
  38.     if (yrepcount>0)    { --yrepcount;  return(lastok);    } else yrepcount=0;
  39.     if (yrepcount==0) { bottomdo(); showplayer(); }    /*    show where the player is    */
  40.     lflush();  
  41.     while (1)
  42.         {
  43.         c[BYTESIN]++;
  44.         if (ckpflag)
  45.           if ((c[BYTESIN] % CHKPTINT) == 0)    /* check for periodic checkpointing */
  46.             {
  47. #ifndef UNIX
  48.             cursors();
  49.             lprcat("\nCheckpointing . . .");
  50.             savegame(ckpfile);
  51.             lprcat("\nDone\n");
  52.             showplayer();
  53.             lflush();
  54.             flushall();    /* Kill any stored key strokes */
  55. #else
  56.             wait(0);    /* wait for other forks to finish */
  57.             if (fork() == 0) { savegame(ckpfile); exit(); }
  58. #endif
  59.  
  60.  
  61. #ifdef TIMECHECK
  62.             if (dayplay==0)
  63.               if (playable())
  64.                 {
  65.                 cursor(1,19);
  66.                 lprcat("\nSorry, but it is now time for work.  Your game has been saved.\n"); beep();
  67.                 lflush();  savegame(savefilename);  wizard=nomove=1;  sleep(4);
  68.                 died(-257);
  69.                 }
  70. #endif TIMECHECK
  71.  
  72.             }
  73.  
  74. # ifdef UNIX
  75.         do        /* if keyboard input buffer is too big, flush some of it */
  76.             {
  77.             ioctl(0,FIONREAD,&ic);
  78.             if (ic>flushno)   read(0,&cc,1);
  79.             }
  80.         while (ic>flushno);
  81. # endif
  82.  
  83. # ifndef UNIX
  84.         cc = getchar();
  85. # else
  86.         if (read(0,&cc,1) != 1) return(lastok = -1);
  87. # endif
  88.  
  89.         if (cc == '!')        /* ! shell escape */
  90.             {
  91.             resetscroll();  clear(); /* scrolling region, home, clear, no attributes */
  92. # ifndef UNIX
  93.             doshell();
  94. # else
  95.             if ((ic=fork())==0) /* child */
  96.                 {
  97.                 execl("/bin/csh",0);    exit();
  98.                 }
  99.             wait(0);
  100.             if (ic<0) /* error */
  101.                 {
  102.                 write(2,"Can't fork off a shell!\n",25); sleep(2);
  103.                 }
  104. # endif
  105.  
  106.             setscroll();
  107.             return(lastok = 'L'-64);    /* redisplay screen */
  108.             }
  109.         
  110.         if ((cc <= '9') && (cc >= '0'))
  111.             { yrepcount = yrepcount*10 + cc - '0'; }
  112.         else    { if (yrepcount>0) --yrepcount;  return(lastok = cc); }
  113.         }
  114.     }
  115.  
  116. /*
  117.  *    flushall()    Function to flush all type-ahead in the input buffer
  118.  */
  119. flushall()
  120.     {
  121. #ifdef MSDOS
  122.     while (kbhit())
  123.         getch();
  124. #else
  125. # ifndef GEMDOS
  126.     char cc;
  127.     int ic;
  128.     for (;;) {    /* if keyboard input buffer is too big, flush some of it */
  129.         ioctl(0,FIONREAD,&ic);
  130.         if (ic<=0)
  131.             return;
  132.         while (ic>0) {
  133.             read(0,&cc,1);
  134.             --ic;
  135.         } /* gobble up the byte */
  136.     }
  137. # endif
  138. #endif
  139. }
  140.  
  141. /*
  142.     function to set the desired hardness 
  143.     enter with hard= -1 for default hardness, else any desired hardness
  144.  */
  145. sethard(hard)
  146. int hard;
  147. {
  148.     register int    j,k;
  149.     long        i;
  150.     struct monst    *mp;
  151.  
  152.     j=c[HARDGAME]; hashewon(); 
  153.     if (restorflag==0)    /* don't set c[HARDGAME] if restoring game */
  154.         {
  155.         if (hard >= 0) c[HARDGAME]= hard;
  156.         }
  157.     else c[HARDGAME]=j; /* set c[HARDGAME] to proper value if restoring game */
  158.  
  159.     if (k=c[HARDGAME])
  160.       for (j=0; j<=MAXMONST+8; j++) {
  161.           mp = &monster[j];
  162.         i = ((6+k) * mp->hitpoints + 1)/6;
  163.         mp->hitpoints = (i<0) ? 32767 : i;
  164.         i = ((6+k) * mp->damage + 1) / 5;
  165.         mp->damage = (i>127) ? 127 : i;
  166.         i = (10 * mp->gold)/(10+k);
  167.         mp->gold = (i>32767) ? 32767 : i;
  168.         i = mp->armorclass - k;
  169.         mp->armorclass = (i< -127) ? -127 : i;
  170.         i = (7*mp->experience)/(7+k) + 1;
  171.         mp->experience = (i<=0) ? 1 : i;
  172.     }
  173. }
  174.  
  175.  
  176. /*
  177.     function to read and process the larn options file
  178.  */
  179. readopts()
  180.     {
  181.     register char *i;
  182.     register int j,k;
  183.     int flag;
  184.     flag=1;        /* set to 0 if a name is specified */
  185.     
  186. # ifdef DGK
  187.     if (plopen(optsfile) < 0)
  188. # else
  189.     if (lopen(optsfile) < 0)
  190. # endif
  191.         {
  192.         strcpy(logname,loginname);
  193.         lprintf("Can't open options file \"%s\"\n", optsfile);
  194.         lflush();
  195.         sleep(2);
  196.         return; /* user name if no character name */
  197.         }
  198.     i = " ";
  199.     while (*i)
  200.       {
  201.       if ((i=(char *)lgetw()) == 0) break; /* check for EOF */
  202.       while (*i && ((*i==' ') || (*i=='\t'))) i++; /* eat leading whitespace */
  203.         if (strcmp(i,"bold-objects") == 0)  boldon=1;
  204.         else if (strcmp(i,"enable-checkpointing") == 0) ckpflag=1;
  205.         else if (strcmp(i,"inverse-objects") == 0)  boldon=0;
  206.         else if (strcmp(i,"female") == 0)    sex=0; /* male or female */
  207. # ifdef DGK
  208.         else if (strcmp(i, "graphics:") == 0) {
  209.             wallc = atoi(lgetw());
  210.             floorc = atoi(lgetw());
  211.             objnamelist[OWALL] = wallc;
  212.  
  213.             /* Set all the invisible objects and monsters
  214.              * to have the same appearance as the floor
  215.              */
  216.             objnamelist[0]          =
  217.             objnamelist[OIVTELETRAP]  =
  218.             objnamelist[OTRAPARROWIV] =
  219.             objnamelist[OIVDARTRAP]      =
  220.             objnamelist[OIVTRAPDOOR]  = floorc;
  221.             monstnamelist[0] =
  222.             monstnamelist[INVISIBLESTALKER] = floorc;
  223.             for (j=DEMONLORD; j<=DEMONPRINCE; j++)
  224.                 monstnamelist[j] = floorc;
  225.         }
  226.         else if (strcmp(i, "larndir:") == 0) {
  227.             if ((i=lgetw())==0)
  228.                 break;
  229.             strncpy(larndir, i, DIRLEN);
  230.             larndir[DIRLEN - 1] = 0;
  231.         }
  232. #ifdef MSDOS
  233.         else if (strcmp(i, "rawio") == 0)
  234.             rawio = 1;
  235. #endif
  236.         else if (strcmp(i, "swapfile:") == 0) {
  237.             if ((i = lgetw()) == 0)
  238.                 break;
  239.             strncpy(swapfile, i, PATHLEN);
  240.             swapfile[PATHLEN - 1] = 0;
  241.         }
  242.         else if (strcmp(i, "ramlevels:") == 0)
  243.             ramlevels = atoi(lgetw());
  244. #ifdef MSDOS
  245.         else if (strcmp(i, "cursor:") == 0) {
  246.             cursorset = 1;
  247.             cursorstart = (unsigned char) atoi(lgetw());
  248.             cursorend = (unsigned char) atoi(lgetw());
  249.         }
  250.         else if (strcmp(i, "keypad") == 0)
  251.             keypad = 1;
  252.         else if (strcmp(i, "DECRainbow") == 0)
  253.             DECRainbow = 1;
  254. #endif
  255. # endif
  256.         else if (strcmp(i,"monster:")== 0)   /* name favorite monster */
  257.             {
  258.             if ((i=lgetw())==0) break;
  259.             if (strlen(i)>=MAXMNAME) i[MAXMNAME-1]=0;
  260.             strcpy(usermonster[usermpoint],i); 
  261.             if (usermpoint >= MAXUM) break; /* defined all of em */
  262.             if (isalpha(j=usermonster[usermpoint][0]))
  263.                 {
  264.                 for (k=1; k<MAXMONST+8; k++) /* find monster */
  265.                   if (monstnamelist[k] == j)
  266.                     {
  267.                     monster[k].name = &usermonster[usermpoint++][0];
  268.                     break;
  269.                     }
  270.                 }
  271.             }
  272.         else if (strcmp(i,"male") == 0)    sex=1;
  273.         else if (strcmp(i,"name:") == 0) /* defining players name */
  274.             {
  275.             if ((i=lgetw())==0) break;
  276.             if (strlen(i)>=LOGNAMESIZE) i[LOGNAMESIZE-1]=0;
  277.             strcpy(logname,i); flag=0;
  278. # ifndef UNIX
  279.             strcpy(loginname,i);
  280. # endif
  281.             }
  282. #ifdef GEMDOS
  283.         else if (strcmp(i,"cmdshell:") == 0)
  284.             {
  285.             if ((i = lgetw()) == 0)
  286.                 break;
  287.             strncpy(cmdshell, i, PATHLEN);
  288.             cmdshell[PATHLEN - 1] = 0;
  289.             }
  290. #endif    
  291.         else if (strcmp(i,"no-introduction") == 0) nowelcome=1;
  292.         else if (strcmp(i,"no-beep") == 0) nobeep=1;
  293. # ifdef UNIX
  294.         else if (strcmp(i,"process-name:")== 0) 
  295.             {
  296.             if ((i=lgetw())==0) break;
  297.             if (strlen(i)>=PSNAMESIZE) i[PSNAMESIZE-1]=0;
  298.             strcpy(psname,i);
  299.             }
  300.         else if (strcmp(i,"play-day-play") == 0)  dayplay=1;
  301. # endif
  302.         else if (strcmp(i,"savefile:") == 0) /* defining savefilename */
  303.             {
  304.             if ((i=lgetw())==0) break;
  305.             if (strlen(i)>=SAVEFILENAMESIZE) /* avoid overflow */
  306.                 i[SAVEFILENAMESIZE-1]=0;
  307.             strcpy(savefilename,i); flag=0;
  308.             }
  309.         else {
  310.             lprintf("Unknown option \"%s\"\n", i);
  311.             lflush();
  312.             sleep(1);
  313.         }
  314.       }
  315.     if (flag)  strcpy(logname,loginname);
  316.     }
  317.